package com.menggudf;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

public class HtmlToPdf {
    public static void main(String[] args) throws IOException, DocumentException {
        String html = "<div style='position: relative; margin-top: 25px; height: 800px;page-break-after: always;margin:0;padding:0;'><table style=\"font-family:arial;\">\n" +
                "   <tr>\n" +
                "        <td width=\"0px\">&nbsp;</td>\n" +
                "        <td valign=\"top\" width=\"626px\">\n" +
                "           <div style=\"position:absolute;\">\n" +
                "   \n" +
                "      <span style='left:356px;top:34px;position:absolute;'><img\n" +
                "               src='%3D'/></span>\n" +
                "\n" +
                " <span style='left:100px;top:0;position:absolute;font-size:12px;'><b>Responsible Delivery - CO<sub>2</sub> Neutral</b></span>\n" +
                " <span style='left:196px;top:88px;position:absolute;font-size:8px;'>Sender</span>\n" +
                " <span style='left:196px;top:96px;position:absolute;font-size:8px;'></span>\n" +
                " <span style='left:196px;top:105px;position:absolute;font-size:8px;'></span>\n" +
                " <span style='left:196px;top:114px;position:absolute;font-size:8px;'></span>\n" +
                " <span style='left:196px;top:123px;position:absolute;font-size:8px;'></span>\n" +
                "<span style='left:196px;top:132px;position:absolute;font-size:8px;'></span>\n" +
                " <span style='left:196px;top:141px;position:absolute;font-size:8px;'></span>\n" +
                "<span style='left:242px;top:145px;position:absolute;font-size:8px;'>Phone:</span>\n" +
                "<span style='left:250px;top:88px;position:absolute;font-size:8px;'>Account:</span>\n" +
                " \n" +
                "   <span style='left:4px;top:19px;position:absolute;font-size:12px;'>AMAZON FULFILLMENT CENTER</span>\n" +
                "<span style='left:4px;top:30px;position:absolute;font-size:12px;'>PLOT 1, LYONS PARK</span>\n" +
                "<span style='left:4px;top:41px;position:absolute;font-size:12px;'></span>\n" +
                " <span style='left:4px;top:52px;position:absolute;font-size:12px;'>SAYER DR COVENTRY</span>\n" +
                " <span style='left:4px;top:63px;position:absolute;font-size:12px;'></span>\n" +
                " <span style='left:4px;top:74px;position:absolute;font-size:12px;'>CV5 9PF</span>\n" +
                " <span style='left:90px;top:74px;position:absolute;font-size:12px;'>United Kingdom</span>\n" +
                "<span style='left:220px;top:68px;position:absolute;font-size:18px;'></span>\n" +
                " <span style='left:4px;top:88px;position:absolute;font-size:8px;'>Contact</span>\n" +
                " <span style='left:4px;top:120px;position:absolute;font-size:8px;'>Consignment</span>\n" +
                " <span style='left:4px;top:128px;position:absolute;font-size:8px;'>Ref</span>\n" +
                " <span style='left:4px;top:104px;position:absolute;font-size:8px;'>Info</span>\n" +
                "\n" +
                "    <span style='left:60px;top:104px;font-size:8px;position:absolute;'></span>\n" +
                "  <span style='left:60px;top:112px;font-size:8px;position:absolute;'></span>\n" +
                "\n" +
                " <span style='left:60px;top:88px;position:absolute;font-size:8px;'></span>\n" +
                " <span style='left:60px;top:120px;position:absolute;font-size:8px;'>1862662333</span>\n" +
                "<span style='left:60px;top:128px;position:absolute;font-size:8px;'></span>\n" +
                " <span style='left:60px;top:136px;position:absolute;font-size:8px;'></span>\n" +
                " <span style='left:60px;top:144px;position:absolute;font-size:8px;'></span>\n" +
                " \n" +
                "<span style='left:284px;top:18px;position:absolute;font-size:8px;'>Packages</span>\n" +
                "<span style='left:276px;top:48px;position:absolute;font-size:8px;'>Total Weight</span>\n" +
                "<span style='left:274px;top:26px;position:absolute;font-size:16px;'>1 of 2</span>\n" +
                "<span style='left:274px;top:56px;position:absolute;font-size:16px;'>2 kg</span>\n" +
                "\n" +
                "<span style='left:6px;top:180px;position:absolute;font-size:8px;'>Track</span>\n" +
                "<span style='left:350px;top:180px;position:absolute;font-size:8px;'>Service</span>\n" +
                "<span style='left:5px;top:158px;position:absolute;font-size:22px;'>1550</span>\n" +
                "<span style='left:62px;top:160px;position:absolute;font-size:16px;'>1862 6623 33F</span>\n" +
                "<span style='left:5px;width:370px;top:250px;position:absolute;font-size:10px;text-align:center;'>      29/11/23 16:01 Web 4.6.10-api      </span>\n" +
                "<span style='left:19px;top:265px;border-top-style:solid;border-width:thin;position:absolute;'><img width='348px' height='95px' src=''/></span>\n" +
                "<span style='left:85px;top:363px;position:absolute;font-size:12px;'>0CV5 9PF1 5501 8626 6233 3812 826W</span>\n" +
                "<span style='right:5px;top:158px;width:250px;text-align:right;position:absolute;font-size:20px;'>          NEXT DAY</span>\n" +
                "<span style='left:130px;top:230px;position:absolute;font-size:14px;'>   812-GB - CV5 9PF   </span>\n" +
                "<span style='left:10px;top:190px;position:absolute;font-size:18px;'></span>\n" +
                "<span style='left:10px;top:225px;position:absolute;font-size:30px;'></span>\n" +
                "<span style='right:10px;top:225px;position:absolute;font-size:30px;'>0065</span>\n" +
                " <span style='left:3px;top:183px;width:380px;font-size:46px;position:absolute;text-align:center;'>GB-CV5 9PF</span>\n" +
                " \n" +
                "                <table style='display:inline-block;border:3px solid black;padding:0;border-spacing:0;border-collapse:collapse;' height='385' width='385px'>\n" +
                "   <tr>\n" +
                "      <td>\n" +
                "         <table style='padding:0;border-spacing:0;border-collapse:collapse;' width='100%'>\n" +
                "              <tr>\n" +
                "                 <td style='font-size:1px;border-bottom:1px solid black;' colspan='4'>\n" +
                "                      <div style='height:10px'>&nbsp;</div>\n" +
                "                 </td>\n" +
                "                </tr>\n" +
                "                <tr>\n" +
                "                 <td colspan='2' style='text-align:left;border-bottom:1px solid black;vertical-align:top;'>\n" +
                "                       <div style='height:69px'>&nbsp;</div>\n" +
                "                    </td>\n" +
                "                    <td style='border-bottom:1px solid black;border-left:1px solid black;'>&nbsp;</td>\n" +
                "                 <td style='border-bottom:3px solid black;border-left:1px solid black;' rowspan='2'>&nbsp;</td>\n" +
                "                </tr>\n" +
                "               <tr>\n" +
                "                    <td style='border-bottom:3px solid black;'>\n" +
                "                        <div style='height:66px'>&nbsp;</div>\n" +
                "                   </td>\n" +
                " <td style='border-bottom:3px solid black;border-left:1px solid black;' colspan='2'>&nbsp;</td>\n" +
                "   </tr>\n" +
                " <tr>\n" +
                "<td style='text-align:left;vertical-align:top;' colspan='4'>\n" +
                "   <div style='height:226px'>&nbsp;</div>\n" +
                "      </td>\n" +
                "    </tr>\n" +
                "         <tr>\n" +
                "           <td style='font-size:1px;' width='195px'>&nbsp;</td>\n" +
                "                  <td style='font-size:1px;' width='70px'>\n" +
                "                   <div style='height:1px'>&nbsp;</div>\n" +
                "           </td>\n" +
                "      <td style='font-size:1px;' width='80px'>&nbsp;</td>\n" +
                "                <td style='font-size:1px;' width='40px'>&nbsp;</td>\n" +
                "               </tr>\n" +
                "           </table>\n" +
                "       </td>\n" +
                "   </tr>\n" +
                "</table>\n" +
                "\n" +
                " </div>\n" +
                "        </td>\n" +
                "  </tr>\n" +
                "</table></div><div style='position: relative; margin-top: 25px; height: 800px;page-break-after: always;margin:0;padding:0;'><table style=\"font-family:arial;\">\n" +
                "    <tr>\n" +
                "       <td width=\"0px\">&nbsp;</td>\n" +
                "        <td valign=\"top\" width=\"626px\">\n" +
                "            <div style=\"position:absolute;\">\n" +
                "    \n" +
                "      <span style='left:356px;top:34px;position:absolute;'><img\n" +
                "                src='%3D'/></span>\n" +
                "\n" +
                " <span style='left:100px;top:0;position:absolute;font-size:12px;'><b>Responsible Delivery - CO<sub>2</sub> Neutral</b></span>\n" +
                "<span style='left:196px;top:88px;position:absolute;font-size:8px;'>Sender</span>\n" +
                "<span style='left:196px;top:96px;position:absolute;font-size:8px;'></span>\n" +
                "<span style='left:196px;top:105px;position:absolute;font-size:8px;'></span>\n" +
                "<span style='left:196px;top:114px;position:absolute;font-size:8px;'></span>\n" +
                "<span style='left:196px;top:123px;position:absolute;font-size:8px;'></span>\n" +
                "<span style='left:196px;top:132px;position:absolute;font-size:8px;'></span>\n" +
                " <span style='left:196px;top:141px;position:absolute;font-size:8px;'></span>\n" +
                "<span style='left:242px;top:145px;position:absolute;font-size:8px;'>Phone:</span>\n" +
                " <span style='left:250px;top:88px;position:absolute;font-size:8px;'>Account:</span>\n" +
                "\n" +
                "    <span style='left:4px;top:19px;position:absolute;font-size:12px;'>AMAZON FULFILLMENT CENTER</span>\n" +
                " <span style='left:4px;top:30px;position:absolute;font-size:12px;'>PLOT 1, LYONS PARK</span>\n" +
                " <span style='left:4px;top:41px;position:absolute;font-size:12px;'></span>\n" +
                " <span style='left:4px;top:52px;position:absolute;font-size:12px;'>SAYER DR COVENTRY</span>\n" +
                "<span style='left:4px;top:63px;position:absolute;font-size:12px;'></span>\n" +
                "<span style='left:4px;top:74px;position:absolute;font-size:12px;'>CV5 9PF</span>\n" +
                "<span style='left:90px;top:74px;position:absolute;font-size:12px;'>United Kingdom</span>\n" +
                "<span style='left:220px;top:68px;position:absolute;font-size:18px;'></span>\n" +
                "<span style='left:4px;top:88px;position:absolute;font-size:8px;'>Contact</span>\n" +
                "<span style='left:4px;top:120px;position:absolute;font-size:8px;'>Consignment</span>\n" +
                "<span style='left:4px;top:128px;position:absolute;font-size:8px;'>Ref</span>\n" +
                " <span style='left:4px;top:104px;position:absolute;font-size:8px;'>Info</span>\n" +
                "\n" +
                " <span style='left:60px;top:104px;font-size:8px;position:absolute;'></span>\n" +
                " <span style='left:60px;top:112px;font-size:8px;position:absolute;'></span>\n" +
                "\n" +
                " <span style='left:60px;top:88px;position:absolute;font-size:8px;'></span>\n" +
                "<span style='left:60px;top:120px;position:absolute;font-size:8px;'>1862662333</span>\n" +
                "<span style='left:60px;top:128px;position:absolute;font-size:8px;'></span>\n" +
                "<span style='left:60px;top:136px;position:absolute;font-size:8px;'></span>\n" +
                " <span style='left:60px;top:144px;position:absolute;font-size:8px;'></span>\n" +
                "\n" +
                " <span style='left:284px;top:18px;position:absolute;font-size:8px;'>Packages</span>\n" +
                " <span style='left:276px;top:48px;position:absolute;font-size:8px;'>Total Weight</span>\n" +
                " <span style='left:274px;top:26px;position:absolute;font-size:16px;'>2 of 2</span>\n" +
                "<span style='left:274px;top:56px;position:absolute;font-size:16px;'>2 kg</span>\n" +
                "\n" +
                "  <span style='left:6px;top:180px;position:absolute;font-size:8px;'>Track</span>\n" +
                "<span style='left:350px;top:180px;position:absolute;font-size:8px;'>Service</span>\n" +
                "<span style='left:5px;top:158px;position:absolute;font-size:22px;'>1550</span>\n" +
                " <span style='left:62px;top:160px;position:absolute;font-size:16px;'>1862 6623 34D</span>\n" +
                "<span style='left:5px;width:370px;top:250px;position:absolute;font-size:10px;text-align:center;'>      29/11/23 16:01 Web 4.6.10-api      </span>\n" +
                "<span style='left:19px;top:265px;border-top-style:solid;border-width:thin;position:absolute;'><img width='348px' height='95px' src=''/></span>\n" +
                "<span style='left:85px;top:363px;position:absolute;font-size:12px;'>0CV5 9PF1 5501 8626 6233 4812 826T</span>\n" +
                "<span style='right:5px;top:158px;width:250px;text-align:right;position:absolute;font-size:20px;'>          NEXT DAY</span>\n" +
                "<span style='left:130px;top:230px;position:absolute;font-size:14px;'>   812-GB - CV5 9PF   </span>\n" +
                "<span style='left:10px;top:190px;position:absolute;font-size:18px;'></span>\n" +
                "<span style='left:10px;top:225px;position:absolute;font-size:30px;'></span>\n" +
                " <span style='right:10px;top:225px;position:absolute;font-size:30px;'>0065</span>\n" +
                "<span style='left:3px;top:183px;width:380px;font-size:46px;position:absolute;text-align:center;'>GB-CV5 9PF</span>\n" +
                "\n" +
                "               <table style='display:inline-block;border:3px solid black;padding:0;border-spacing:0;border-collapse:collapse;' height='385' width='385px'>\n" +
                "     <tr>\n" +
                "        <td>\n" +
                "             <table style='padding:0;border-spacing:0;border-collapse:collapse;' width='100%'>\n" +
                "                <tr>\n" +
                "                   <td style='font-size:1px;border-bottom:1px solid black;' colspan='4'>\n" +
                "                        <div style='height:10px'>&nbsp;</div>\n" +
                "                     </td>\n" +
                "                 </tr>\n" +
                "                 <tr>\n" +
                "                    <td colspan='2' style='text-align:left;border-bottom:1px solid black;vertical-align:top;'>\n" +
                "                     <div style='height:69px'>&nbsp;</div>\n" +
                "                    </td>\n" +
                "                     <td style='border-bottom:1px solid black;border-left:1px solid black;'>&nbsp;</td>\n" +
                "                    <td style='border-bottom:3px solid black;border-left:1px solid black;' rowspan='2'>&nbsp;</td>\n" +
                "                 </tr>\n" +
                "                 <tr>\n" +
                "                    <td style='border-bottom:3px solid black;'>\n" +
                "                       <div style='height:66px'>&nbsp;</div>\n" +
                "                      </td>\n" +
                "                    <td style='border-bottom:3px solid black;border-left:1px solid black;' colspan='2'>&nbsp;</td>\n" +
                "               </tr>\n" +
                "                <tr>\n" +
                "                    <td style='text-align:left;vertical-align:top;' colspan='4'>\n" +
                "                      <div style='height:226px'>&nbsp;</div>\n" +
                "                 </td>\n" +
                "               </tr>\n" +
                "               <tr>\n" +
                "                  <td style='font-size:1px;' width='195px'>&nbsp;</td>\n" +
                "                   <td style='font-size:1px;' width='70px'>\n" +
                "                        <div style='height:1px'>&nbsp;</div>\n" +
                "                   </td>\n" +
                "                 <td style='font-size:1px;' width='80px'>&nbsp;</td>\n" +
                "                    <td style='font-size:1px;' width='40px'>&nbsp;</td>\n" +
                "                </tr>\n" +
                "            </table>\n" +
                "         </td>\n" +
                "</tr>\n" +
                "</table>\n" +
                "\n" +
                "           </div>\n" +
                "       </td>\n" +
                "     </tr>\n" +
                "</table></div>";
        // 将 HTML 转换为 PDF 字节数组
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

        Document document = new Document();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PdfWriter.getInstance(document, baos);
        document.open();
        document.add(new Paragraph(html));
        document.close();
        byte[] pdfBytes = baos.toByteArray();


    }
}